//
// List of frames and what they correspond to (for Client Pull):
//
// window.top.frames[0] = topic frame, where the MOTD for the room is displayed
// window.top.frames[1] = content frame, where the chat messages are displayed
// window.top.frames[2] = avatar frame, where the user list is displayed
// window.top.frames[3] = submit frame, where the user types messages to send
// window.top.frames[4] = settings frame, where pause/mute choices are written to
//                        and the MID and URL are stored
// window.top.frames[5] = events frame, where we poll for new events
//

function A(msg) {
    //
    // Create a new div, set the contents with the message, and pop it at the end of the chain... scroll appropriately.
    //
    var bodyDoc = window.top.frames[1].document;
    var newDiv = bodyDoc.createElement("div");
    newDiv.innerHTML = msg;
    bodyDoc.body.appendChild(newDiv);

    if (window.top.frames[4].scrollPolicy == "auto") {
        window.top.frames[1].scrollTo(0, 999999999);
    }
}

function S(url) {
    //
    // If sound is not muted, create a new HTML embed tag with the url passed and send it to the client to play once.
    //
    if (window.top.frames[4].mutePolicy != "muted") {
        var bodyDoc = window.top.frames[1].document;
        var newDiv = bodyDoc.createElement("div");
        newDiv.innerHTML = "<embed autostart=true loop=1 playcount=1 src="+url+" hidden=true>";
        bodyDoc.body.appendChild(newDiv);
    }
}

function clearChat() {
    //
    // On client request, clean the chat buffer.
    //
    var bodyDoc = window.top.frames[1].document.body;
    var numNodes = bodyDoc.childNodes.length;
    var thisNode;

    //
    // New Method (only removes child divs, leaves body.innerHTML mods alone....
    //
    for (thisNode = numNodes - 1; thisNode >= 0; thisNode--) {
        var myNode = bodyDoc.childNodes[thisNode];

        if (myNode.tagName == "DIV") {
            bodyDoc.removeChild(bodyDoc.childNodes[thisNode]);
        }
    }

    var newDiv = window.top.frames[1].document.createElement("div");
    newDiv.innerHTML = "<B>*** Chat buffer cleared.</b><br />";
    bodyDoc.appendChild(newDiv);
}

function changeTopic(msg) {
    window.top.frames[0].document.body.innerHTML=msg;
}

function STC(msg, flags) {
    //
    // flags specifies certain actions:
    //
    // 0: process chat event normally
    // 1: process chat event and reload frame 0 (topic)
    // 2: process chat event and reload frame 2 (avatars)
    // 3: process chat event and reload frames 0 and 2 (topic and avatars)
    // 4: process sound event
    //
    if (flags == 4) {
        S(msg);
        setFocus();
    } else {
        A(msg);

        if (flags == 1) {
            window.top.frames[0].location.reload();
        } else if (flags == 2) {
            window.top.frames[2].location.reload();
        } else if (flags == 3) {
            window.top.frames[0].location.reload();
            window.top.frames[2].location.reload();
        }
    }
}

function addCSS(cssToAdd) {
    var bodyDoc = window.top.frames[1].document.body;
    bodyDoc.innerHTML   += cssToAdd;
}

function toggleScrolling() {
    //
    // Pause / unpause scrolling...
    //
    var mainFrame = window.top.frames[4];
    var thisButton = document.forms.msg.pause;
    //alert(mainFrame.scrollPolicy);
    if (mainFrame.scrollPolicy == "auto") {
        mainFrame.scrollPolicy = "paused";
        thisButton.value = "Unpause";
    } else {
        mainFrame.scrollPolicy = "auto";
        thisButton.value = "Pause";
    }
    //alert(mainFrame.scrollPolicy);
}

function toggleMute() {
    //
    // Mute / unmute sounds...
    //
    var mainFrame = window.top.frames[4];
    var thisButton = document.forms.msg.mute;
    //alert(mainFrame.mutePolicy);
    if (mainFrame.mutePolicy == "full") {
        mainFrame.mutePolicy = "muted";
        thisButton.value = "Unmute";
    } else {
        mainFrame.mutePolicy = "full";
        thisButton.value = "Mute";
    }
    //alert(mainFrame.mutePolicy);
}

function setButtons() {
    //
    // Set the name of the buttons appropriately after the user
    // sends a message to chat.
    //
    var mainFrame = window.top.frames[4];
    var muteButton = document.forms.msg.mute;
    var pauseButton = document.forms.msg.pause;
    if (mainFrame.mutePolicy == "full") {
        muteButton.value = "Mute";
    } else {
        muteButton.value = "Unmute";
    }
    if (mainFrame.scrollPolicy == "auto") {
        pauseButton.value = "Pause";
    } else {
        pauseButton.value = "Unpause";
    }
}

function setMID(mid) {
    //
    // Store the value of the last message ID so we can tell if we've already seen
    // previous messages.
    //
    //console.log("setMID("+mid+")");
    try {
        var mainFrame = window.top.frames[4];
        var midDiv = mainFrame.document.getElementById('midDiv');

        mainFrame.mid = mid;
        midDiv.innerHTML = "MID is <b>" + mid + "</b>";
    } catch (e) {
        //console.log("Failed to setMID("+mid+"), sleeping for 1000ms.");
        t = setTimeout("setMID("+mid+")", 1000);
    }
}

function setURL(url) {
    //
    // Set URL to poll from. We do this repeatedly in case we want to stop processing,
    // like when someone logs out.
    //
    //console.log("setURL("+url+")");
    try {
        var mainFrame = window.top.frames[4];
        var urlDiv = mainFrame.document.getElementById('urlDiv');

        mainFrame.baseURL = url;
        urlDiv.innerHTML = "Base URL is <b>" + url + "</b>";
    } catch (e) {
        //console.log("Failed to setURL("+url+"), sleeping for 1000ms.");
        t = setTimeout("setURL('"+url+"')", 1000);
    }
}

function getNewEvents() {
    //
    // Poll for new events: forum a URL based on the base URL and message ID, and refresh
    // the events page appropriately. Do not refresh if the baseURL is empty, as the user
    // has likely logged out.
    //
    var pullFrame = window.top.frames[5];
    var mainFrame = window.top.frames[4];

    var pullURL = mainFrame.baseURL + '&mid=' + mainFrame.mid + '&cpsp=1';
    var errURL = '&mid=' + mainFrame.mid + '&cpsp=1';

    if (errURL != pullURL) {
        //alert("Loading: " + pullURL);
        pullFrame.document.location = pullURL;
    }
}

function setFocus() {
    //
    // Set focus to the message input box for user convenience.
    //
    document.msg.message.focus();
}

function smilie(smile) {
    //
    // Add appropriate smilie code from the quick bar to the message input box and set focus
    // back to the input box.
    //
    document.msg.message.value += ' ' + smile + ' ';
    document.msg.message.focus();
}

function MLPopup(sid) {
    //
    // Create the CBRTE popup to handle rich text pasting.
    //
    var w = 500, h = 300,
    move = screen ?  ',left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '',
    o_mlWin = window.open('ml_new.php?sid='+sid, 'pastebox', "help=no,status=no,scrollbars=no,resizable=no" + move + ",width=" + w + ",height=" + h + ",dependent=yes", true);
    o_mlWin.opener = window;
    o_mlWin.focus();
}

function MultiLineEditor() {
    this.show = document.layers ?
        function (div) { this.divs[div].visibility = 'show' } :
        function (div) { this.divs[div].visibility = 'visible' };
    this.hide = document.layers ?
        function (div) { this.divs[div].visibility = 'hide' } :
        function (div) { this.divs[div].visibility = 'hidden' };
    // event handlers
    this.popup   = MLPopup;
}

